Imports DotNetNuke.Application
Imports DotNetNuke.Entities.Portals
Imports DnnSecurity.Results
Imports System.Net
Imports System.Xml

Namespace Checks
    Public Class UpToDate
        Inherits MultilingualBase
        Implements IDnnSecurityCheck

        Const CodeplexUrl As String =
            "http://dotnetnuke.codeplex.com/project/feeds/rss?ProjectRSSFeed=codeplex%3a%2f%2frelease%2fdotnetnuke"


        Public ReadOnly Property DisplayName As String Implements IDnnSecurityCheck.DisplayName
            Get
                Return GetTranslationFor (Me.GetType.Name & ".DisplayName")
            End Get
        End Property

        Public ReadOnly Property ShortInfoText() As String Implements IDnnSecurityCheck.ShortInfoText
            Get
                Return GetTranslationFor (Me.GetType.Name & ".ShortInfoText")
            End Get
        End Property

        Public ReadOnly Property Description() As String Implements IDnnSecurityCheck.Description
            Get
                Return GetTranslationFor (Me.GetType.Name & ".Description")
            End Get
        End Property

        Public ReadOnly Property SecurityRiskClassification As SecurityRiskClassification _
            Implements IDnnSecurityCheck.SecurityRiskClassification
            Get
                Dim res As IDnnSecurityResult =
                        Results.OrderByDescending (Function(x) x.SecurityRiskClassification).FirstOrDefault
                If res IsNot Nothing Then
                    Return res.SecurityRiskClassification
                End If

                Return SecurityRiskClassification.Ok
            End Get
        End Property

        Private ReadOnly _results As New List(Of IDnnSecurityResult)

        Public ReadOnly Property Results() As List(Of IDnnSecurityResult) Implements IDnnSecurityCheck.Results
            Get
                Return _results
            End Get
        End Property

        Public Sub Execute (ByVal portalSettings As PortalSettings, ByVal result As IAsyncResult) _
            Implements IDnnSecurityCheck.Execute
            'UpToDate
            Dim currentVersion As Version = DotNetNukeContext.Current.Application.Version
            Results.Add (New CurrentVersion (currentVersion.ToString))

            Dim res As String = UpgradeIndicator()
            'DotNetNuke.Services.Upgrade.Upgrade.UpgradeIndicator(currentVersion, True, True)
            If res <> "" Then
                Results.Add (New UpdateAvailable (HttpUtility.HtmlDecode (res)))
            End If
        End Sub

        Public Overloads Sub LoadRessourceFile (ByVal fileName As String) Implements IDnnSecurityCheck.LoadRessourceFile
            MyBase.LoadRessourceFile (fileName)
        End Sub

        Public Shared Function UpgradeIndicator() As String
            Dim client As New WebClient

            Try
                Dim releaseNews As String = client.DownloadString (CodeplexUrl)

                Dim xmlDoc As New XmlDocument
                xmlDoc.LoadXml (releaseNews)

                Dim releaseNodes As XmlNodeList = xmlDoc.SelectNodes ("rss/channel/item")

                For Each releaseNode As XmlNode In releaseNodes
                    Return releaseNode.OuterXml
                Next
            Catch ex As Exception

            End Try


            Return ""

        End Function
    End Class
End Namespace

